package expression.v1.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import expression.v1.config.ExpConfig;
import expression.v1.jsondata.ExpPackage;
import expression.v1.jsondata.ExpPackageDetail;
import expression.v1.model.ExpImage;
import expression.v1.model.ExpPkg;
import expression.v1.service.IExpImageService;
import expression.v1.service.IExpPackageService;

@Controller
public class ExpressionController {

	// 表情包详情基本URL
	private static final String EXP_DETAIL_BASEURL = "/exp/detail.do";

	@Autowired
	ExpConfig expConfig;
	
	@Resource
	private IExpPackageService expPkgService;
	@Resource
	private IExpImageService expImgService;

	@ResponseBody
	@RequestMapping("/exp/newest.do")
	public List<ExpPackage> newest(HttpServletRequest request/*,
			@RequestParam int pageSize, @RequestParam int pageNow*/) {
		
		  /*System.out.println(request.getServletPath()); 
		  ExpPackage expPkg = new ExpPackage();
		  expPkg.setName("liubaexp");
		  expPkg.setUrl("/resources/butterfly.jpg");
		  expPkg.setDetail("/exp/detail.do");*/
		 
		List<ExpPkg> expPkgs = expPkgService.queryLatest(0,10);
		List<ExpPackage> retData = new ArrayList<ExpPackage>();
		// String expDetailBaseUrl = "/exp/detail.do";
		for (ExpPkg pkg : expPkgs) {
			ExpPackage expPkg = new ExpPackage();
			expPkg.setName(pkg.getKeyword());
			expPkg.setUrl(pkg.getPkgImg());
			expPkg.setDetail(EXP_DETAIL_BASEURL + "?keyword="
					+ pkg.getKeyword());
			retData.add(expPkg);
		}
		return retData;
	}

	@ResponseBody
	@RequestMapping("/exp/detail.do")
	public List<ExpPackageDetail> detail(
			@RequestParam(value = "keyword", required = true)
			String keyword) {
		List<ExpImage> imgs = expImgService.queryImgsByPkgKeyword(keyword,0,10);
		List<ExpPackageDetail> list = new ArrayList<ExpPackageDetail>();
		for (ExpImage expImg : imgs) {
			ExpPackageDetail detail = new ExpPackageDetail();
			detail.setName(expImg.getName());
			detail.setDetail(expConfig.getMapping() + expImg.getPkg() + "/"
					+ expImg.getName());
			list.add(detail);
		}
		return list;
	}
}
